<template>
  <a-table :columns="columns" :data-source="data">
    <template #headerCell="{ column,record }">
      <template v-if="column.key === 'title'">
        <span>
        {{ record.title }}
        </span>
      </template>
    </template>

    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'description'">
        <a>
          {{ record.description }}
        </a>
      </template>
      <template v-else-if="column.dataIndex === 'priority'">
        <div v-if="record.priority===0">
          <a-tag color="red">高</a-tag>
        </div>
        <div v-else-if="record.priority===1">
          <a-tag color="blue">中</a-tag>
        </div>
        <div v-else>
          <a-tag color="green">低</a-tag>
        </div>
      </template>

      <template v-else-if="column.dataIndex === 'status'">
        <div v-if="record.status===0">
          <a-tag color="red">进行中</a-tag>
        </div>
        <div v-else-if="record.status===1">
          <a-tag color="blue">已完成</a-tag>
        </div>
        <div v-else>
          <a-tag color="green">过期</a-tag>
        </div>
      </template>
      <template v-else-if="column.dataIndex==='createTime'">
        {{ dayjs(record.createTime).format('YYYY-MM-DD') }}
      </template>
      <template v-else-if="column.dataIndex==='deadTime'">
        {{ dayjs(record.deadTime).format('YYYY-MM-DD') }}
      </template>
      <template v-else-if="column.dataIndex==='action'">
        <a-space wrap>
          <a-button :href="`/update_task?id=${record.id}`" ghost link target="_self" type="link">
            编辑
          </a-button>
          <a-button danger type="link" @click="doDelete(record.id)">
            删除
          </a-button>
        </a-space>
      </template>
    </template>
  </a-table>
</template>
<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { message } from 'ant-design-vue'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { taskGetTaskListUsingPost } from '@/api/taskController.ts'
import dayjs from 'dayjs'

const columns = [
  {
    title: '任务标题',
    dataIndex: 'title'
  },
  {
    title: '任务描述',
    dataIndex: 'description'
  },
  {
    title: '任务优先级',
    dataIndex: 'priority'
  },
  {
    title: '任务状态',
    dataIndex: 'status'
  },
  {
    title: '创建时间',
    dataIndex: 'createTime'
  },
  {
    title: '截止时间',
    dataIndex: 'deadTime'
  },
  {
    title: '操作',
    dataIndex: 'action'
  }
]

const data = ref([])
const total = ref(0)
const loginUserStore = useLoginUserStore()


const fetchData = async () => {
  const res = await taskGetTaskListUsingPost(
    {
      userId: loginUserStore.loginUser.id
    }
  )
  if (res.data.code === 0 && res.data.data) {
    data.value = res.data.data
    total.value = res.data.data.length
  } else {
    message.error('获取数据失败')
  }
}

onMounted(() => {
  fetchData()
})
</script>

